/**
 * @file {cf_pol目标库flaw表model}
 */
 'use strict'

 const {Model} = require('../../../azeroth').sequelize;
 const {panelInitData} = require('../common/init_data');
 class PanelPol extends Model { }
 
 /**
  * @desc 映射需要的字段 【源库的 部分字段 --> 目标库字段】
  */
  const initPanelData = (sequelize_aim) => {
    PanelPol.init(
         ...panelInitData,
         {
             freezeTableName: true,
             sequelize: sequelize_aim,
             modelName: 'panel',
             timestamps: false
         }
     )
  }
  PanelPol.getNewsPanelInfoFromLineDb = async (sequelize_aim, panelId) => {
    let sql = 'select * from panel where panel_id = ' + panelId;
    let res = await sequelize_aim.query(sql, { type: sequelize_aim.QueryTypes.SELECT });
    return res[0];
  }
  /**
   * 查询源表的最新位置
   * @param {*} sequelize_source 
   * @returns 
   */
  PanelPol.getMaxUidfromSourcePanel = async (sequelize_source) => {
    let sql = 'select max(sheet_id) as uid from pol_aoi';
    let res = await sequelize_source.query(sql, { type: sequelize_source.QueryTypes.SELECT });
    return res[0].uid;
  }
  /**
   * 插叙目标库同步的位置
   * @param {*} sequelize_aim 
   * @returns 
   */
  PanelPol.getLatestPanelId = async (sequelize_aim) => {
    let sql = 'select max(uid) as uid from panel';
    let res = await sequelize_aim.query(sql, { type: sequelize_aim.QueryTypes.SELECT });
    return res[0].uid;
  }
  /**
   * 查询最新一条的 panel的信息
   * @param {sequelize} sequelize_source
   * @returns
   */
  PanelPol.getLatestPanel = async (sequelize_source, queryStr) => {
    let query = typeof(queryStr) == 'string' ? `b.panel_id = "${queryStr}"` : `a.uid = ${queryStr}`;

    let sql =  "select a.* , b.panel_id from sheet a inner join pol_aoi b on a.uid = b.sheet_id where " + query;
    let res = await sequelize_source.query(sql, { type: sequelize_source.QueryTypes.SELECT });
    return res[0]; //TODO: 暂时的逻辑是  panel <--> sheet  一对一
  }
  /**
   * 上传panel
   * @param {Object} data 上传的panel信息对象
   * @returns
   */
  PanelPol.uploadPolInfo = async (data) => {
    let res = await PanelPol.bulkCreate({...data}, {
      updateOnDuplicate: ['flaw_count','is_ok', 'length_md', 'length_cd', ' save_path', ' gen_time', ' gen_time_str', 'detectParam', 'checked']
    });
    return res;
  }
 module.exports = {
     PanelPol,
     initPanelData
 };